package com.hnjowe.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import com.union.core.doc.service.OfficialDocManagerService;
import com.union.core.organization.service.OrganizationManagerService;
import com.union.core.organization.service.PersonnelService;
import com.union.core.sys.service.SysManagerService;

public class ComUtil {
	private static OfficialDocManagerService officialDocService;
	private static SysManagerService sysManagerService;
	private static PersonnelService personnelService;
	private static OrganizationManagerService orgManagerService;
	private static long seed = new Date().getTime();
	private static final int BUFFER_SIZE = 16 * 1024;
	/**
	 * 公文上传目录
	 */
	public final static String DOC_UPLOAD_DIR = "/upload/doc/";

	public static int boolToInt(boolean b) {
		if (b) {
			return 1;
		} else {
			return 0;
		}
	}

	public static OfficialDocManagerService getOfficialDocService() {
		if (officialDocService == null) {
			officialDocService = new OfficialDocManagerService();
		}
		return officialDocService;
	}

	public static SysManagerService getSysManagerService() {
		if (sysManagerService == null) {
			sysManagerService = new SysManagerService();
		}
		return sysManagerService;
	}

	public static PersonnelService getPersonnelService() {
		if (personnelService == null) {
			personnelService = new PersonnelService();
		}
		return personnelService;
	}

	public static OrganizationManagerService getOrgManagerService() {
		if (orgManagerService == null) {
			orgManagerService = new OrganizationManagerService();
		}
		return orgManagerService;
	}

	public synchronized static String genFileId() {
		String fileId = seed + "";
		seed++;
		return fileId;
	}

	public static void copyFile(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		if (pos > 0) {
			return fileName.substring(pos);
		} else {
			return "";
		}
	}
}
